package cn.zktr.Personnel.model.service.zhouyongcong.monit;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zktr.Personnel.model.pojos.PerAuditShlx;
import cn.zktr.Personnel.model.pojos.PerPmsDept;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;


@Service
@Transactional(rollbackFor=Exception.class)
public class monitgetService {
	
	@Autowired
	private findService fs;
	@PersistenceContext
	private EntityManager entityManager;
	
	public void getPojos(Object obj) {
		Session session = entityManager.unwrap(org.hibernate.Session.class);
		session.evict(obj);
	}
	
	public PerPmsEmp getPerPmsEmp(PerPmsEmp ppe,PerAuditShlx pas) {
		int id = Integer.valueOf(pas.getShlxxq().split(":")[0]);
		int ig = Integer.valueOf(pas.getShlxxq().split(":")[1]);
		
		if(ppe.getPerPmsStms().get(0).getPerPmsPot().getPotid()!=id) {
			if(id>0) {
				return fs.findPotid(id).getPerPmsStms().get(0).getPerPmsEmp();
			}else if(id==0) {
				List<PerPmsDept> list =  fs.findAllDept();
				for(PerPmsDept ppd : list) {
					if(ppe.getPerPmsStms().get(0).getPerPmsDept().getDeptss()==ppd.getDeptid()) {
						return ppd.getPerPmsMgms().get(0).getPerPmsEmp();
					}
				}
				return ppe;
			}else if(id==-1) {
				return fs.findEmpid(1);
			}
		}else if(ppe.getPerPmsStms().get(0).getPerPmsPot().getPotid()==id){
			if(ig==0) {
				List<PerPmsDept> list =  fs.findAllDept();
				for(PerPmsDept ppd : list) {
					if(fs.findPotid(ig).getPerPmsBzbs().get(0).getPerPmsDept().getDeptid()==ppd.getDeptss()) {
						return ppd.getPerPmsMgms().get(0).getPerPmsEmp();
					}
				}
				return ppe;
			}else if(ig>0){
				return fs.findPotid(ig).getPerPmsStms().get(0).getPerPmsEmp();
			}else {
				return ppe;
			}
		}
		return ppe;
	}
}
